package com.hk.service.impl;

import com.hk.commons.entity.EncourageHistory;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IEncourageHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Sheldon Chen on 2017/2/27.
 */
@Service
public class EncourageHistoryImpl extends BaseServiceImpl<EncourageHistory> implements IEncourageHistory {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(EncourageHistory model) {
        super.save(model);
    }

    @Override
    public List<Long> findAllOrderId() {
        String sql = "select orderId from encourage_history";
        List<Map<String, Object>> results = this.jdbcTemplate.queryForList(sql);
        List<Long> orderList = new ArrayList<>();
        for (Map<String, Object> result : results) {
            orderList.add((Long)result.get("orderId"));
        }
        return orderList;
    }

    @Override
    public Integer findByOrderId(Long orderId) {
        String countSql = "select count(*) as count from encourage_history where orderId = ?";
        Integer count = this.jdbcTemplate.queryForObject(countSql, new Object[] { orderId }, Integer.class);
        return count;
    }
}
